package org.callisto;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.callisto.midi.MIDIInterface;


public class Callisto {
	
	private static String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        return dateFormat.format(date);
    }
	
	public static void main(String[] arg) throws Exception {
		try{		
			if (arg.length < 1) {
				throw new Exception("Required argument missing");
			}
			EnumMode mode = EnumMode.RECORD;
			Globals.setDebugFlag(false);
			int matchCount = 0;
			for (int argIndex=0; argIndex <= arg.length - 1; argIndex++) {
				if (arg[argIndex].equalsIgnoreCase("--mode=PLAY")) {
					mode = EnumMode.PLAY;
					matchCount++;
				} else if (arg[argIndex].equalsIgnoreCase("--mode=RECORD")) {
					mode = EnumMode.RECORD;
					matchCount++;
				} else if (arg[argIndex].equalsIgnoreCase("--debug")) {
					Globals.setDebugFlag(true);
					matchCount++;
				} else if (arg[argIndex].startsWith("--handlers=")) {
					String handlers = "";
					handlers=arg[argIndex].replace("--handlers=","");
					Globals.setHandlers(handlers);
					matchCount++;
				}
			}
			MIDIInterface midi = new MIDIInterface();
			System.out.print("Starting Callisto 1.02");			
			System.out.println(getDateTime());
			final StringBuffer buffer = new StringBuffer();
			buffer.append("TIME NOTE STATUS PRESSURE\n" );
			switch (mode) {
			case PLAY: 
				System.out.println("Play mode");
				midi.getSequence();
				break;
			case RECORD:
				System.out.println("Record mode");
				midi.listen();
				boolean stay=true;
				//TODO define STAY (above)
				while (stay) Thread.sleep(5000);
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e);
		}
	}
}
